package com.zheng.shop.rpc.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zheng.common.annotation.BaseService;
import com.zheng.common.base.BaseServiceImpl;
import com.zheng.shop.common.exception.ShopException;
import com.zheng.shop.dao.mapper.ShopBrandMapper;
import com.zheng.shop.dao.model.ShopBrand;
import com.zheng.shop.dao.model.ShopBrandCategory;
import com.zheng.shop.rpc.api.ShopBrandCategoryService;
import com.zheng.shop.rpc.api.ShopBrandService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author likun
 * @since 2017-12-29
 */
@Service
@Transactional(rollbackFor = ShopException.class)
@BaseService
public class ShopBrandServiceImpl extends BaseServiceImpl<ShopBrandMapper, ShopBrand> implements ShopBrandService {
    private static Logger _log = LoggerFactory.getLogger(ShopBrandServiceImpl.class);

    @Autowired
    private ShopBrandMapper brandMapper;
    @Autowired
    private ShopBrandCategoryService brandCategoryService;

    @Override
    public boolean createBrand(ShopBrand brand) throws ShopException {
        return false;
    }

    @Override
    public boolean updateBrand(ShopBrand brand) throws ShopException {
        return false;
    }

    @Override
    public boolean deleteBrand(List<Integer> ids) throws ShopException {
        List<ShopBrand> brandList = super.selectBatchIds(ids);
        for (ShopBrand brand : brandList) {
            EntityWrapper<ShopBrandCategory> brandCategoryEntityWrapper = new EntityWrapper<>();
            brandCategoryEntityWrapper.where("brand_id=" + brand.getId());
            if (!brandCategoryService.delete(brandCategoryEntityWrapper)) {
                throw new ShopException("删除品牌关联失败!");
            }
        }
        for (ShopBrand brand : brandList) {
            if (!deleteById(brand.getId())) {
                throw new ShopException("删除品牌失败！");
            }
        }
        return true;
    }

    @Override
    public boolean updateSort(List<ShopBrand> brandList) throws ShopException {
        ShopBrand brand;
        for (int i = 0; i < brandList.size(); i++) {
            brand = brandList.get(i);
            brand.setSort(i + 1);
            if (!updateById(brand)) {
                throw new ShopException("修改排序失败！");
            }
        }
        return true;
    }

    @Override
    public int selectMaxSort() {
        return 0;
    }

    @Override
    public List<Map<String, Object>> selectBrandCategoryMap(Integer brandId) {
        return brandMapper.selectBrandCategoryMap(brandId);
    }

    @Override
    public boolean updateBrandCategory(Integer brandId, List<Integer> productCategoryIds) throws ShopException {
        //修改品牌分类
        EntityWrapper<ShopBrandCategory> brandCategoryEntityWrapper = new EntityWrapper<>();
        brandCategoryEntityWrapper.where("brand_id=" + brandId);
        if (!brandCategoryService.delete(brandCategoryEntityWrapper)) {
            throw new ShopException("删除品牌关联失败！");
        }

        ShopBrandCategory brandCategory;
        for (Integer id : productCategoryIds) {
            brandCategory = new ShopBrandCategory();
            brandCategory.setBrandId(brandId);
            brandCategory.setProductCategoryId(id);
            if (!brandCategoryService.insert(brandCategory)) {
                throw new ShopException("分类失败！");
            }
        }
        return true;
    }

}
